package com.chenxinzhimusic.ui.page

import android.Manifest
import android.os.Build
import androidx.annotation.RequiresApi
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import com.google.accompanist.permissions.ExperimentalPermissionsApi
import com.google.accompanist.permissions.rememberPermissionState

@Composable
fun NotificationPermissionRequestDialog(
    onRequestPermission: () -> Unit,
    onDismiss: () -> Unit,
    content: String = "需要开启通知权限以便接收下载进度消息，如无法展示授权证明您已经永久拒绝,请前往设置开启,是否允许？",

) {
    AlertDialog(
        onDismissRequest = onDismiss,
        title = { Text("权限申请") },
        text = { Text(content) },
        confirmButton = {
            TextButton(onClick = {
                onRequestPermission()
                onDismiss()
            }) {
                Text("允许")
            }
        },
        dismissButton = {
            TextButton(onClick = onDismiss) {
                Text("拒绝")
            }
        }
    )
}

@OptIn(ExperimentalPermissionsApi::class)
@RequiresApi(Build.VERSION_CODES.TIRAMISU)
@Composable
fun RequestNotificationPermissionWithConfirm(showDialog: Boolean,close:()-> Unit) {
    val cameraPermissionState = rememberPermissionState(
        Manifest.permission.POST_NOTIFICATIONS
    )
    if (showDialog) {
        NotificationPermissionRequestDialog(
            onRequestPermission = {
                cameraPermissionState.launchPermissionRequest()
                close()
            },
            onDismiss = {
                close()
            }
        )
    }
}
